package org.dromara.hm.service.impl;

import cn.hutool.core.date.DateUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.hm.domain.bo.HmStockChangeBo;
import org.dromara.hm.domain.vo.HmStockChangeVo;
import org.dromara.hm.domain.HmStockChange;
import org.dromara.hm.mapper.HmStockChangeMapper;
import org.dromara.hm.service.IHmStockChangeService;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 库存变更明细Service业务层处理
 *
 * @author hzy
 * @date 2025-04-01
 */
@RequiredArgsConstructor
@Service
public class HmStockChangeServiceImpl implements IHmStockChangeService {

    private final HmStockChangeMapper baseMapper;

    /**
     * 查询库存变更明细
     *
     * @param changeId 主键
     * @return 库存变更明细
     */
    @Override
    public HmStockChangeVo queryById(Long changeId){
        return baseMapper.selectVoById(changeId);
    }

    /**
     * 分页查询库存变更明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 库存变更明细分页列表
     */
    @Override
    public TableDataInfo<HmStockChangeVo> queryPageList(HmStockChangeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HmStockChange> lqw = buildQueryWrapper(bo);
        Page<HmStockChangeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的库存变更明细列表
     *
     * @param bo 查询条件
     * @return 库存变更明细列表
     */
    @Override
    public List<HmStockChangeVo> queryList(HmStockChangeBo bo) {
        LambdaQueryWrapper<HmStockChange> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HmStockChange> buildQueryWrapper(HmStockChangeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HmStockChange> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStockId()!= null, HmStockChange::getStockId, bo.getStockId());
        lqw.eq(bo.getChangeNum() != null, HmStockChange::getChangeNum, bo.getChangeNum());
        lqw.ge(bo.getStartTime()!= null, HmStockChange::getChangeTime, bo.getStartTime());
        lqw.le(bo.getEndTime()!= null, HmStockChange::getChangeTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeInfo()), HmStockChange::getChangeInfo, bo.getChangeInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getChangeType()), HmStockChange::getChangeType, bo.getChangeType());
        lqw.eq(StringUtils.isNotBlank(bo.getProductId()), HmStockChange::getProductId, bo.getProductId());
        lqw.like(StringUtils.isNotBlank(bo.getProductName()), HmStockChange::getProductName, bo.getProductName());
        lqw.eq(StringUtils.isNotBlank(bo.getProof()), HmStockChange::getProof, bo.getProof());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorId()), HmStockChange::getOperatorId, bo.getOperatorId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperator()), HmStockChange::getOperator, bo.getOperator());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordStartLocation()), HmStockChange::getRecordStartLocation, bo.getRecordStartLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getRecordEndLocation()), HmStockChange::getRecordEndLocation, bo.getRecordEndLocation());
        lqw.like(StringUtils.isNotBlank(bo.getRecordStartLocationName()), HmStockChange::getRecordStartLocationName, bo.getRecordStartLocationName());
        lqw.like(StringUtils.isNotBlank(bo.getRecordEndLocationName()), HmStockChange::getRecordEndLocationName, bo.getRecordEndLocationName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HmStockChange::getStatus, bo.getStatus());
        lqw.orderByDesc(HmStockChange::getChangeTime);
        return lqw;
    }

    /**
     * 新增库存变更明细
     *
     * @param bo 库存变更明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HmStockChangeBo bo) {
        HmStockChange add = MapstructUtils.convert(bo, HmStockChange.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setChangeId(add.getChangeId());
        }
        return flag;
    }

    /**
     * 修改库存变更明细
     *
     * @param bo 库存变更明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HmStockChangeBo bo) {
        HmStockChange update = MapstructUtils.convert(bo, HmStockChange.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HmStockChange entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除库存变更明细信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
